package com.yanqu.road.server.cmd.activity.tongbing.cross;

import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.task.args.activity.TongBingRedPacketValueArgs;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigInteger;
import java.util.Map;


@Cmd(code = GameProtocol.S_CROSS_TONG_BING_RED_PACKET_GET_BOX, desc = "")
public class TongBingRedPacketGetBoxCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        TongBingProto.CrossTongBingRedPacketGetBoxRespMsg reqMsg = TongBingProto.CrossTongBingRedPacketGetBoxRespMsg.parseFrom(packet.getBytes());

        String reward = "";

        GamePlayer player = GamePlayerMgr.getOnlinePlayer(packet.getPlayerId());
        if (player != null) {

            if (reqMsg.getRet() == 0) {

                // 奖励全部打开吧
                Property openResult = new Property();
                Property property = PropertyHelper.parseStringToProperty(reqMsg.getReward());
                for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
                    int boxId = entry.getKey();
                    long count = entry.getValue().longValue();
                    // 宝箱一个个开
                    for (int i = 0; i < count; i++) {
                        OpenGoodsBagResult randomGoods = GoodsBagMgr.getRandomGoods(boxId);
                        openResult.addProperty(randomGoods.getReward());
                    }
                }

                // 设置奖励
                reward = PropertyHelper.parsePropertyToString(openResult);

                // 加奖励
                player.getModule(CurrencyModule.class)
                        .addCurrency(openResult, eLogMoneyType.TongBing, eLogMoneyType.TongBingRedPacketGetBox);
                player.notifyListenerAndSync(eGamePlayerEventType.TongBingCampBoxScore.getValue(), new TongBingRedPacketValueArgs(reqMsg.getActivityId(), reqMsg.getBoxValue()));
            }

            // 返回
            TongBingProto.TongBingRedPacketGetBoxRespMsg.Builder respMsg = TongBingProto.TongBingRedPacketGetBoxRespMsg.newBuilder();
            respMsg.setRet(reqMsg.getRet());
            respMsg.setReward(reward);
            player.sendPacket(ClientProtocol.U_TONG_BING_RED_PACKET_GET_BOX, respMsg);
        }
    }

}
